package com.zhao.shiyuanbaoyou.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhao.shiyuanbaoyou.R;
import com.zhao.shiyuanbaoyou.adapter.WorthRcAdapter;
import com.zhao.shiyuanbaoyou.bean.WorthBean;
import com.zhao.shiyuanbaoyou.interfacepackage.IViewPager;
import com.zhao.shiyuanbaoyou.urlutil.Constant;
import com.zhao.shiyuanbaoyou.util.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 张昭 on 2016/11/12.
 */

public class WorthFragmentItem extends Fragment{
    private RecyclerView recyclerView_worth;
    private Context mContext;
    private List<WorthBean.RowsBean> mList = new ArrayList<>();
    private WorthRcAdapter mAdapter;
    private int index = 0;
    private Observable<WorthBean> observable;

    public WorthFragmentItem(int index) {
        this.index = index;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.item_fragment_worth,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView_worth= (RecyclerView) view.findViewById(R.id.recyclerView_fragment_worth);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initGetData();
    }

    private void initGetData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.Worth_URL).
                        addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        IViewPager iViewPager = retrofit.create(IViewPager.class);
        switch (index){
            case 0:
                observable = iViewPager.getJsonWorth0String();
                break;
            case 1:
                observable = iViewPager.getJsonWorth1String();
                break;
            case 2:
                observable = iViewPager.getJsonWorth2String();
                break;
            case 3:
                observable = iViewPager.getJsonWorth3String();
                break;
            case 4:
                observable = iViewPager.getJsonWorth4String();
                break;
            case 5:
                observable = iViewPager.getJsonWorth5String();
                break;
            case 6:
                observable = iViewPager.getJsonWorth6String();
                break;
            case 7:
                observable = iViewPager.getJsonWorth7String();
                break;
            case 8:
                observable = iViewPager.getJsonWorth8String();
                break;
            case 9:
                observable = iViewPager.getJsonWorth9String();
                break;
            case 10:
                observable = iViewPager.getJsonWorth10String();
                break;
        }
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<WorthBean>() {
                    @Override
                    public void call(WorthBean worthBean) {
                        mList=worthBean.getRows();
                        recyclerView_worth.setLayoutManager(new GridLayoutManager(mContext, 2));
                        recyclerView_worth.setHasFixedSize(true);
                        recyclerView_worth.addItemDecoration(new SpacesItemDecoration(3));
                        mAdapter = new WorthRcAdapter(mContext, mList);
                        recyclerView_worth.setAdapter(mAdapter);
                    }
                });
    }
}
